package com.tools.common.utils.spring;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * Spring 工具类
 * @author Admin
 */
@Component
public class SpringContextUtils implements ApplicationContextAware
{
    private static ApplicationContext applicationContext = null;

    @Override
    public void setApplicationContext(ApplicationContext context)throws BeansException
    {
        if (applicationContext == null)
        {
            applicationContext = context;
        }
    }

    public static ApplicationContext getApplicationContext()
    {
        return applicationContext;
    }

    public static <T> T getBean(Class<T> clazz)
    {
        return getApplicationContext().getBean(clazz);
    }

    public static final Object getBean(String beanName)
    {
        return getApplicationContext().getBean(beanName);
    }
}
